<?php
declare (strict_types = 1);
namespace app\logic;

use app\model\HandleCompany;
use app\model\HandleProject;

class ProjectLogic
{

    public static function ins() {
        static $cls;
        if (!$cls) {
            $cls = new static();
        }
        return $cls;
    }

    /**
     * 添加项目
     * @param string $title
     * @return array
     */
    public function addProject($title)
    {
        $title = trim($title);
        if (empty($title)) {
            return errorMsg('未知项目名称');
        }
        // if (!Lock::ins()->lock('project:add')) {
        //     return ['code'=>399,'msg'=>'请勿频繁请求'];
        // }
        $exist = HandleProject::ins()->where('state', 1)
            ->where('title', $title)
            ->value('id');
        if ($exist > 0) {
            return errorMsg('项目名称已存在');
        }
        $data = [
            'uuid' => getGuid(),
            'title' => $title,
            'state' => 1
        ];
        $result = HandleProject::ins()->insertGetId($data);
        if ($result > 0) {
            return successMsg('操作成功');
        } else {
            return errorMsg('操作失败');
        }
    }

    /**
     * 获取项目列表
     * @return array
     */
    public function getProjectList()
    {
        $list = HandleProject::ins()->where('state', 1)
            ->order('id desc')
            ->paginate(['list_rows'=>10]);
        return $list;
    }

    /**
     * 获取项目信息
     * @param string $uuid
     * @return array|null
     */
    public function getProjectInfo($uuid) {
        if (empty($uuid)) {
            return null;
        }
        $info = HandleProject::ins()->where('state', 1)
            ->where('uuid', $uuid)
            ->findOrEmpty()->toArray();
        if (empty($info)) {
            return null;
        }
        return $info;
    }

    /**
     * 添加甲方信息
     * @param array $data
     * @return array
     */
    public function addCompany($data)
    {
        $insert_data = [
            'company_name'=>$data['name'],
            'clean_type_data'=>json_encode(array_combine($data['file_code'], $data['file_type']),JSON_UNESCAPED_UNICODE) 
        ];
        $resule = HandleCompany::ins()->insertGetId($insert_data);
        if($resule > 0){
            return successMsg('操作成功');
        }else{
            return errorMsg('操作失败');
        }
    }

    /**
     * 获取甲方列表
     * @return array
     */
    public function getCompanyList()
    {
        $list = HandleCompany::ins()->where('state', 1)
            ->order('id desc')
            ->paginate(['list_rows'=>10]);
            foreach ($list as $key => $value) { 
                $data = json_decode($value['clean_type_data'],true);
                $list[$key]['file_code'] = array_keys($data);
                $list[$key]['file_type'] = array_values($data);
            }
        return $list;
    }

    public function editCompany($data) 
    {
        $update_data = [
            'clean_type_data'=>json_encode(array_combine($data['file_code'], $data['file_type']),JSON_UNESCAPED_UNICODE) 
        ];
        $resule = HandleCompany::ins()->where('id', $data['id'])
            ->update($update_data);
        if($resule > 0){
            return successMsg('操作成功');
        }else{
            return errorMsg('操作失败');
        }
    }
}